package cdu.bbs.bbsv2.controller;

import cdu.bbs.bbsv2.domain.Admin;
import cdu.bbs.bbsv2.domain.Topic;
import cdu.bbs.bbsv2.domain.User;
import cdu.bbs.bbsv2.service.AdminService;
import cdu.bbs.bbsv2.service.TopicService;
import cdu.bbs.bbsv2.service.UserService;
import cdu.bbs.bbsv2.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired private AdminService adminService;
    @Autowired private UserService userService;
    @Autowired private TopicService topicService;

    @RequestMapping("/login")
    @ResponseBody
    public Map<String, Object> login(String loginAct, String loginPwd, HttpServletRequest request, HttpServletResponse response) {

        loginPwd = MD5Util.getMD5(loginPwd);    // 密码加密
        Admin admin = adminService.login(loginAct, loginPwd);
        Map<String,Object> map = new HashMap<>();

        if (admin != null) { // 查找到管理员账户
            request.getSession().setAttribute("admin", admin);
            map.put("success",true);

        } else { // 未找到该管理员
            map.put("success", false);
            map.put("msg", "账号或密码不正确");
        }
        return map;
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request, HttpServletResponse response) {
        request.getSession().invalidate();
        return "loginPage";
    }


    @RequestMapping("/userList")
    public ModelAndView userList(String name, String loginAct,  String email){
        Map<String,Object> map = new HashMap<>();

        Map<String,String> conditions = new HashMap<>();
        conditions.put("name",name);
        conditions.put("loginAct",loginAct);
        conditions.put("email",email);

        List<User> usersByCondition = userService.getUsersByCondition(conditions);
        map.put("users",usersByCondition);

        return new ModelAndView("admin/userList",map);
    }

    @RequestMapping("/deleteUserById")
    @ResponseBody
    private Map<String, Object> deleteUserById(String id){

        Boolean flag = userService.deleteUserById(id);

        Map<String,Object> map = new HashMap<>();
        map.put("success",flag);
        return map;
    }
    @RequestMapping("/deleteUserByIds")
    @ResponseBody
    private Map<String, Object> deleteUserByIds( String[] ids){

        Boolean flag = userService.deleteUserByIds(ids);

        Map<String,Object> map = new HashMap<>();
        map.put("success",flag);
        return map;

    }

    @RequestMapping("/getUserById")
    @ResponseBody
    private Map<String, Object> getUserById(String id){

        User u = userService.getUserById(id);

        Map<String,Object> map = new HashMap<>();
        map.put("user",u);
        System.out.println(u);
        return map;
    }

    @RequestMapping("/modUserById")
    @ResponseBody
    public Map<String, Object> modUserById(User user) {
        user.setLoginPwd(MD5Util.getMD5(user.getLoginPwd()));    // 密码加密
        user.setLockState("1");

        boolean flag = userService.modUserById(user);

        Map<String,Object> map = new HashMap<>();
        map.put("success",flag);
        return map;
    }

    /* 帖子管理相关代码开始*/
    @RequestMapping("/topicList")
    public ModelAndView topicList(String title, String createBy,  String createTime){
        Map<String,Object> map = new HashMap<>();

        Map<String,String> conditions = new HashMap<>();
        conditions.put("title",title);
        conditions.put("createBy",createBy);
        conditions.put("createTime",createTime);
        List<Topic> topicsByCondition = topicService.getTopicsByCondition(conditions);


        map.put("topicsByCondition",topicsByCondition);
        return new ModelAndView("admin/topicList",map);
    }

    /* 帖子管理相关代码结束*/

}




